package com.mars.controller;

import com.google.common.collect.Maps;
import com.mars.beans.PageQuery;
import com.mars.beans.PageResult;
import com.mars.common.JsonData;
import com.mars.common.RequestHolder;
import com.mars.dto.AclModuleLevelDto;
import com.mars.param.DeptParam;
import com.mars.param.UserParam;
import com.mars.pojo.SysUser;
import com.mars.service.SysMenuService;
import com.mars.service.SysRoleService;
import com.mars.service.SysTreeService;
import com.mars.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 用户管理
 * @author MARS
 * @date 2018/1/7
 */

@Controller
@RequestMapping("/sys/user")
@Slf4j
public class SysUserController {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysTreeService sysTreeService;
    @Resource
    private SysMenuService sysMenuService;
    @Resource
    private SysRoleService sysRoleService;

    @RequestMapping("/noAuth.page")
    public ModelAndView noAuth(){
        return new ModelAndView("noAuth");
    }
    /**
     * 新增用户
     * @param param
     * @return
     */
    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData saveUser(UserParam param){
        sysUserService.save(param);
        return JsonData.success();
    }

    /**
     * 更新用户
     * @param param
     * @return
     */
    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData updateUser(UserParam param){
        sysUserService.update(param);
        return JsonData.success();
    }

    @RequestMapping("/page.json")
    @ResponseBody
    public JsonData page(@RequestParam("deptId")int deptId , PageQuery pageQuery) {
        // 返回分页后的数据
        PageResult<SysUser> result = sysUserService.getPageByDeptId(deptId,pageQuery);
        return JsonData.success(result);
    }

    /**
     * 获取菜单tree
     * @return
     */
    @RequestMapping("/menu.json")
    @ResponseBody
    public JsonData menuTree(){
        List<AclModuleLevelDto> aclModuleLevelDtoList = sysTreeService.userAclTree(RequestHolder.getCurrentUser().getId());
        return JsonData.success(sysMenuService.changeTreeToMenu(aclModuleLevelDtoList));
    }

    /**
     * 根据用户获取权限和角色
     * @param userId
     * @return
     */
    @RequestMapping("/acls.json")
    @ResponseBody
    public JsonData acls(@RequestParam("userId") int userId) {
        Map<String,Object> map = Maps.newHashMap();
        // 传入用户的权限点
        map.put("acls",sysTreeService.userAclTree(userId));
        // 传入用户角色
        map.put("roles",sysRoleService.getRoleListByUserId(userId));
        return JsonData.success(map);
    }
}
